import { YopService } from '@/core/yop.service';
import { CurrentUser, Public } from '@/decorators';
import { halt, ok } from '@/utils';
import { Body, Controller, Get, Post } from '@nestjs/common';
import { BaseController } from './base.controller';
import { SkipThrottle } from '@nestjs/throttler';

@Controller('yop')
export class YopController extends BaseController {
  constructor(private readonly yopService: YopService) {
    super();
  }

  @Public()
  @Post('wallet/callback')
  @SkipThrottle()
  async walletCallback(@Body() dto: any) {
    console.log('钱包注册回调通知:', dto);
    return 'SUCCESS';
  }

  @Post('wallet')
  async wallet(@CurrentUser('id') userId: number) {
    const user = await this.prisma.user.findUnique({ where: { id: userId } });
    if (!user) {
      return halt('用户信息异常');
    }
    if (!user.isReal) {
      return halt('请先完成实名认证');
    }

    const res = await this.yopService.wallet({
      requestNo: await this.yopService.generateOrderNo(),
      userNo: `${user.id}-${user.code}`,
      name: user.realname,
      idCard: user.idCard,
      mobile: user.mobile,
      returnUrl: `${this.yopService.ReturnUrl}`,
      notifyUrl: `${this.yopService.NotifyUrl}`,
    });

    return ok(res);
  }

  @Public()
  @Get('order')
  async order() {
    return ok(
      await this.yopService.order({
        orderId: await this.yopService.generateOrderNo(),
        orderAmount: 0.01,
        goodsName: `测试藏品: ${await this.yopService.generateOrderNo()}`,
        returnUrl: `${this.yopService.ReturnUrl}`,
        notifyUrl: `${this.yopService.NotifyUrl}/api/yop/order/callback`,
      }),
    );
  }

  @Public()
  @Get('test')
  async test() {
    const orderId = '1013202404240000011298842058';
    // const data = {
    //   response:
    //     'cP4SRrh3FK7gyBGB_8fid3LlsW59YPqbM7IUAqgEVvmBOwEwKv6AWplp8xGuxU4whV8VOPgSvUoVDk53S8eoIXIKECZn1bcEKyW2mHN46tevXBIF9CdIT5ldYIKuDuuT93daiqxldH7i088Av6xagMjm07VwyzTEXQJ8yZhgAIdqjwA318QRnumF9DTSWo4UAGnsusKKufLU00uYCKfN7ifKDVgDGonI6RonUXyAf7a8_AD9XSohWpkclHUC-pe1Pt3wPnCPPxumeOEt-QEtKxMV_ByHBMr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1C81GccK89vXublIpA01dY7_ZsxciVEK-OGK3l6OtrNHLijgigwHCWbNofUR-GEI9WG3Fq0aFI0NsWerFgXLHyJmgPRfFgTMlgVwvFjj48akyr-TJiTFQ0YJQxqgOMTUci2bsSSI1jSuF0pMfvLNA60ScBge_zrvozDjj9lAgnbom-sf3CECMyt0d4YIIW7cAfAvJeq8atBgeYqZ9WHk4KwqrZ_dhRunUZIrPCvAchiK-LafcF8IgX_wafP6r572YW7IeShHEFmZyCGWHA$AES$SHA256',
    //   customerIdentification: 'app_10089037520',
    // };

    const data = {
      response:
        'XSL-V2VYcRfllnVP_GZSH5VGLYkLyDYWk6vreEnb4_3EHYO8IzBMkO7sVkX41hogtxm8XW2cHAdF6cQPqiN04Ro-G_vHqsS_LNxVSN5MdN74UfVeJREpuFdjzlcH4udSKHFxdqAEzc0mENRtrPLtg0YATk5D5g7pxHjXd4ydhimQogvJ3GsouocBSPBEuwXGNY11ylsoK5cOLwpTFS6YvgAaB3d5lLgLB2U8Aa3n5fMejfnQ_XQUJMSdTZ8QI0bgGY5h-d5Khs9zA6lWXKs_8HtPwHapL5g3Gg5eyvKXmS9_0DcU9xA7ylPLqCqVdKjvI58xclM0h9mjaCE7cAEfXw$dVw3oUqBDQatfrGAiG9PmBJZs1qGTR3EM6Y2QVTNAqsyIyNZmBlMhhTUQQ8k4m3wNvNtegwd-N7OlV0VXIJiQpNencyXsqH2I-sae9lyvcmDeLjofPRU1XOBr-_WcUdejbOaOuMt8cdnoM4cMeFsYEAYwxhTfDy2QYqzkNHsRDFNU2YwFEOK4es8d32xiczvInch9n0AG5QAGKTbvrnYTsBIc6H89f5rFnANWiXyPOJer_iE4rd6t-wc1yBwZVtnV0fQgQA3NEnHZ2YOEmHaqQxQBlqkbSGTL-_zqiFmtU4f_V6A44cYzdrS8MfDbOSNabbVqpUj2cz4ffhMxsQbSgJDsBZybZxy41AcCXbR1e1khVWXBmQX2mK6HV1etUCE8x6c5QFmmxFTxwffuUMewVWk7UYQ6pV34_0wTN3vu9TxmIkMWEBnR382BPVK5JHgYSbk-_yuLhSYFhqbXQZJ-2HxTHrQXBTXFjXTWiIeIEWbttRLBcDtOlw9hQOtd1Vixo78uX8U8zoHztY3ZqdLqW7KntbJQTZ3hGguz39qYFupNsaW0Wc6fjl93IDaadpOQL4fBaUIgeLdmg_bRV8OrHV3AJDKjUxJl3X-l_6pxRoLGBRdgbSxRhactzAtnwdoRdssA0DLsXQieaEPC_IfIdQBGs-AcEy3_7AU5eE-eLOdhaLbdS-ZnBV3aiG2NLWOzhRjAlab4RHPLhiNvll3b4d6Ktcm5U7SOoGbcLXxSQbgEYXAMKmF07OV3ZnNfQ6mYSbk-_yuLhSYFhqbXQZJ-w5EXnC0fo9O1oFv3Af9S0bue7eKKitIQg7evVrlPzvw2Mfa7gnzC1majAQDYBinLQNS-jhRHrm1eUEJmz7JF_3xlo3N8lGCN5EgyAbm8QNsIkKWuLNd8YdFKbWO0YTm3pRCZ51ZJZCvE9NKGGG6r2iqi4p3hiYQ5RzI7Rqnjg6Q3p_bRu2xSAh7z7eUV4pms45YK-k-LAiLKDLnR0h3Ed--ptGDqCddpAnYjvKM00iYo8ifFObwUn3feE7drqoBfuEsPIRVYPObfhtujgLNoBPA5oR8J8BdZQgIrPeSv0PSgqCpmBOkI_MIIDJ4uLuDErTdeRAt5shtaRZhYqwL30KRMP9i2fuM53IZYf-UO1zevKfAjjX4ej-JgCJrLlKwvkjEE8kCY55gCzoMn4xCf6w958RgLNBE1He-gJEYGsctDcr2LW42hZ-5uIB0lXf7pH1KepD_86Mtb7KLq4_0jSqoEmrEuImAhFQcBvhj53QD3tkpxNseZF6qi_PODn_ftaVq9CNPlulX9pYFP6f0_IIY8Pwe3Cv9mq8xC2WJxy7pV_rPs2PbW5cEa34l4DKcDqo2llSFAWUXaU8OFy6LJlE$AES$SHA256',
      customerIdentification: 'app_10089037520',
    };

    const signData = await this.yopService.verifySign(data.response);
    console.log('\n', signData, '\n');

    const res = await this.yopService.memberQuery('4-avkt45sf');
    console.log('用户钱包信息:', res);

    return ok(res);
    // return ok(
    //   await this.yopService.wallet({
    //     requestNo: await this.yopService.generateOrderNo(),
    //     userNo: `2-hfxmayhp`,
    //     name: '林志霄',
    //     idCard: '360426199604254019',
    //     mobile: '18770221825',
    //     returnUrl: `${this.yopService.ReturnUrl}`,
    //     notifyUrl: `${this.yopService.NotifyUrl}/api/yop/wallet/callback`,
    //   }),
    // );
  }
}
